package com.hins.linkedList;

public class SingleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode heroNode1=new HeroNode(1,"宋江","及时雨");
        HeroNode heroNode2=new HeroNode(2,"卢俊义","玉麒麟");
        HeroNode heroNode3=new HeroNode(3,"吴用","智多星");
        HeroNode heroNode4=new HeroNode(4,"林冲","豹子头");
        //HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
        //HeroNode heroNode4 = new HeroNode(4, "林冲", "豹子头");
        //HeroNode heroNode3 = new HeroNode(3, "吴用", "智多星");
        //HeroNode heroNode2 = new HeroNode(2, "卢俊义", "玉麒麟");

        SingleLinkedList singleLinkedList = new SingleLinkedList();
        //singleLinkedList.addByOrder(heroNode1);
        //singleLinkedList.addByOrder(heroNode2);
        //singleLinkedList.addByOrder(heroNode3);
        //singleLinkedList.addByOrder(heroNode4);
       
        singleLinkedList.update(new HeroNode(5, "小宋", "及时雨"));
        singleLinkedList.del(8);
        singleLinkedList.List();
    }

}

//定义一个SingleLinkedList，来管理英雄

class SingleLinkedList {
    //先初始化一个头结点，头结点不能动
    private HeroNode head = new HeroNode(0, "", "");

    //添加节点到单向链表
    public void add(HeroNode heroNode) {
        //思路：当不考虑编号顺序时
        //1.找到当前链表的最后节点
        //2.将这个节点的next指向新的节点
        //因为head节点不能动，因此我们需要一个辅助节点temp来遍历链表
        HeroNode temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        //当退出while循环时，temp就指向了链表的最后一个节点
        temp.next = heroNode;
    }

    //第二种方式再添加英雄时，根据排名插入到指定位置
    //(如果有这个no，则添加失败，给出提示)
    public void addByOrder(HeroNode heroNode) {
        //单链表节点不能动，继续使用辅助指针来帮助找到要添加的位置
        //我们要找的temp是添加节点的上一个节点
        HeroNode temp = head;
        while (true) {

            //如果temp的下一个节点为空则直接添加到temp节点上即可
            if (temp.next == null) {
                temp.next = heroNode;
                break;
            } else if (temp.next.no > heroNode.no) {
                //找到添加位置
                heroNode.next = temp.next;
                temp.next = heroNode;
            } else if (temp.next.no == heroNode.no) {
                System.out.printf("要添加的节点%d已经存在，请重新添加", heroNode.no);
                break;
            }

            temp = temp.next;
        }

    }

    //修改节点的信息，根据no来修改，那么no不能修改
    public void update(HeroNode heroNode) {
        //判断是否为空
        if (head.next == null) {
            System.out.println("链表为空，无法修改");
        }
        HeroNode temp = head.next;
        //boolean flag = false;
        while (true) {
            if (temp==null){
                break;
            }else 
            if (temp.no == heroNode.no) {
                //修改
                temp.name = heroNode.name;
                temp.nikename = heroNode.nikename;
                break;
            }
            temp = temp.next;
        }
            System.out.printf("没有找到编号为%d的节点,不能修改\n", heroNode.no);
        //if (flag){
        //    temp.name = heroNode.name;
        //    temp.nikename = heroNode.nikename;
        //}else {
        //    System.out.printf("没有找到编号为%d的节点,不能修改\n",heroNode.no);
        //}
    }

    //显示链表
    public void List() {
        //判断链表是否为空

        if (head.next == null) {
            System.out.println("链表为空");
           // return;
        }
        HeroNode temp = head.next;
        while (true) {
            //判断是否到链表最后
            if (temp == null) {
                break;
            }

            System.out.println(temp);
            //将temp后移
            temp = temp.next;
        }


    }

    //删除节点(根据no删除节点)
    public void del(int no) {
        
        HeroNode temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no == no) {
                //删除节点
                
                temp.next = temp.next.next;
                break;
            }
            temp = temp.next;

        }
            System.out.printf("没有找到要删除的%d节点\n", no);
    }

}


//定义一个HeroNode，每一个HeroNode对象就是一个节点
class HeroNode {
    public int no;
    public String name;
    public String nikename;
    public HeroNode next;//指向下一个节点

    //构造器
    public HeroNode(int no, String name, String nikename) {
        this.no = no;
        this.name = name;
        this.nikename = nikename;
    }
    //为了显示方便，我们重写一下tostring方法


    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nikename='" + nikename + '\'' +

                '}';
    }
}
